<?php
/**
 * FastApp.
 * 7/27/24
 * @Author 深色的海 (wx:deepsea_0024)
 * @Contact telegram:deepsea159
 */

namespace App\Fastapp\Api\Service\Account;

use App\Fastapp\Api\Service\BaseService;
use App\Fastapp\Api\Service\Wechat\MiniService;
use App\Fastapp\Model\AccountInfo;
use App\Fastapp\Model\AccountInviteLevel;
use App\Fastapp\Model\AccountShare;
use App\Fastapp\Model\AccountWalletLog;
use Kkokk\Poster\Facades\Poster;
use Kkokk\Poster\Image\Builder;

class ShareService extends BaseService
{
    public function create(array $data): AccountShare
    {
        $data['user_id'] = get_uid();
        if (empty($data['n'])) {
            $md = AccountShare::query()->where(['user_id' => $data['user_id'], 'type' => $data['type']])->first();
            if ($md) return $md;
        }
        return AccountShare::query()->create($data);
    }

    public function detail(int $id): AccountShare
    {
        return AccountShare::query()->where(['id' => $id])->firstOrFail();
    }

    public function poster(array $param): string
    {
        $param['user_id'] = empty($param['user_id']) ? get_uid() : $param['user_id'];
        $param['filename'] = md5('poster' . $param['user_id']) . '.png';
        $param['path'] = '/poster/' . date('Ymd') . '/' . $param['filename'];
        $param['file_path'] = BASE_PATH . '/public/' . $param['path'];
        $poster = Poster::extension('imagick')->config([
            'path' => BASE_PATH . '/public/poster/',
        ]);
        if ($param['bg_type'] == 1) $this->bg1($poster, $param);
        $res = $poster->getPoster($param['file_path']);
        return file_get_contents($res['url']);
    }

    public function bg1(Builder &$poster, array &$param): void
    {
        $bg_file = BASE_PATH . '/public/poster/temp/T1-' . $param['bg_no'] . '.png';
        $poster->buildImDst($bg_file);
        $info = AccountInfo::query()->where(['user_id' => $param['user_id']])->first(['avatar', 'nickname', 'gender']);
        if ($info->avatar) $poster->buildImage(BASE_PATH . '/public' . parse_url($info->avatar)['path'], 30, 110, 0, 0, 50, 50);
        else {
            $def_avatar = BASE_PATH . '/public/poster/temp/';
            if ($info->gender == 2) $def_avatar .= 'def_avatar2.png'; else $def_avatar .= 'def_avatar1.png';
            $poster->buildImage($def_avatar, 30, 110, 0, 0, 50, 50);
        }
        $poster->buildText($info->nickname, 95, 115, 20, [0, 0, 0, 1], weight: 2);

        $poster->buildText('长按识别二维码加入', 25, 580, 16, [151, 151, 151, 1]);
        $poster->buildQr(json_encode([
            'path' => $param['path'],
            'user_id' => $param['user_id'],
        ]), 210, 480);
        $poster->buildText($info->nickname, 150, 150, 20, [255, 255, 255, 1]);
    }


    public function wxmini(array $param): string
    {
        $param['filename'] = md5(get_uid() . serialize($param)) . 'png';
        $service = new MiniService();
        return $service->qrcode($param);
    }

    public function invite(): array
    {
        $paginate = AccountInviteLevel::query()
            ->with(['account:gene_id,username,email,mobile,created_at'])
            ->select(['user_id'])
            ->where(['parent_id' => get_uid()])->orderByDesc('id')->simplePaginate();
        $list = [];
        foreach ($paginate->items() as $item) {
            $list[] = $item->account;
        }
        return ['list' => $list];
    }

    public function dashboard(): array
    {
        $uid = get_uid();
        $uids = AccountInviteLevel::query()
            ->with(['account:gene_id,username,email,mobile,created_at'])
            ->select(['user_id'])
            ->where(['parent_id' => $uid])->pluck('user_id');
        $data['total_num'] = $uids->count();
        $data['total_prize'] = AccountWalletLog::query()
            ->where(['user_id' => $uid, 'object_type' => AccountWalletLog::OBJ_TYPE_INVITE])
            ->sum('amount');
        $data['total_recharge_num'] = AccountWalletLog::query()
            ->whereIn('user_id', $uids)
            ->where(['object_type' => AccountWalletLog::OBJ_TYPE_RECHARGE])
            ->groupBy('user_id')
            ->pluck('user_id')->count();
        return $data;
    }

    public function income(): array
    {
        $uid = get_uid();
        $paginate = AccountWalletLog::query()
            ->select(['id', 'amount', 'created_at'])
            ->where(['user_id' => $uid, 'object_type' => AccountWalletLog::OBJ_TYPE_INVITE])
            ->orderByDesc('id')
            ->simplePaginate();
        return ['list' => $paginate->items()];
    }
}